package gqz.mvvm.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

import java.util.ArrayList;
import java.util.List;

import gqz.mvvm.R;
import gqz.mvvm.adapter.MyFragmentPagerAdapter;
import gqz.mvvm.view.custom.ViewPagerIndicator;
import gqz.mvvm.view.fragment.MyFragment;

/**
 * Created by Administrator on 2017/12/5.
 */

public class ViewPagerActivity extends FragmentActivity {

    private ViewPager vp;
    private ViewPagerIndicator indicator;
    private MyFragmentPagerAdapter adapter;

    private List<String> titles = new ArrayList<>();//= Arrays.asList("Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8", "Item9", "Item10");
    private ArrayList<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);
        vp = (ViewPager) findViewById(R.id.viewpager_vp);
        indicator = (ViewPagerIndicator) findViewById(R.id.viewpager_indicator);

        for (int i = 1; i <= 10; i++) {
            titles.add("Item" + i);
        }
        for (String title : titles) {
            MyFragment fragment = MyFragment.newInstance(title);
            fragments.add(fragment);
        }

        adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments);
//        vp.setAdapter(adapter);
//        vp.setCurrentItem(0);

        //设置Tab上的标题
        indicator.setVisibleTabCount(5);//此方法要在setTabItemTitles()之前调用
        indicator.setTabItemTitles(titles);
        vp.setAdapter(adapter);
        //设置关联的ViewPager
        indicator.setViewPager(vp, 0);
    }
}
